﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;

namespace sharpCmd.EasterEggs
{
    internal class Snake
    {
        public Snake()
        {
            points = new List<PointF>();
            HeadLocation = new PointF(200, 350);
            points.Add(HeadLocation);
            angle = Math.PI / 2;
        }

        const double collisionDistance = 2;
        private const double rotationStep = Math.PI/13;
        public List<PointF> points;
        public double angle;
        public DirectionEnum Direction;
        public PointF HeadLocation;

        /// <summary>
        /// Move for 1 timestep
        /// </summary>
        public bool Move()
        {
            Rotate();
            //add 1 new point 2 pixels away from our location
            const double speed = 3;
            double dx = Math.Cos(angle) * speed;
            double dy = Math.Sin(angle) * speed;
            HeadLocation = new PointF(HeadLocation.X - (float)dx, HeadLocation.Y - (float)dy);
            //check for collisions
            var pts = points.Where(p=>Math.Sqrt( (p.X-HeadLocation.X)*(p.X-HeadLocation.X)+(p.Y-HeadLocation.Y)*(p.Y-HeadLocation.Y)) < collisionDistance);
            if(pts.Count() > 0)
                return false;
            points.Add(HeadLocation);
            return true;
        }

        private void Rotate()
        {
            if (Direction == DirectionEnum.Left)
            {
                angle -= rotationStep;
            }
            else if (Direction == DirectionEnum.Right)
            {
                angle += rotationStep;
            }
        }

        public enum DirectionEnum
        {
            None,
            Left,
            Right
        }
    }
}
